package com.whpost.service.dao.t;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

public interface MyDao extends BaseMapper {
    
    //获取系统日期
    @Select("select sysdate from dual")
    String getRealSysdate();

    @Select("select to_char(sysdate,'yyyy-MM-dd') from dual")
    String getSysdate();

    //获取系统日期
    @Select("select to_char(sysdate,'yyyymmddhh24mmss') from dual")
    String getRq();

    @Select("select to_char(sysdate,'yyyymmdd') from dual")
    String getRqyyyymmdd();

    //插入nonce，有效时间20分钟，先延长，后期优化再考虑提交前获取？
    @Insert("insert into t_api_nonce (czy_id,ms,nonce,end_date) values (#{czyId},#{ms},#{nonce},sysdate+20/24/60)")
    int addNonceByUser(@Param(value = "czyId") String czyId,@Param(value = "ms") String ms,@Param(value = "nonce") String nonce);

    //获取有效期内的nonce
    @Select("<script>select nonce from t_api_nonce where <if test='nonce!=null'>nonce=#{nonce} and </if> czy_id=#{czyId} and ms=#{ms} and end_date>=sysdate and rownum=1</script>")
    String getNonceByUser(@Param(value = "czyId") String czyId,@Param(value = "ms") String ms,@Param(value = "nonce") String nonce);

    //删除过期的nonce
    @Delete("delete from t_api_nonce where czy_id=#{czyId} and ms=#{ms} and end_date<sysdate")
    int delNonceByUser(@Param(value = "czyId") String czyId,@Param(value = "ms") String ms);

    //删除使用的nonce
    @Delete("delete from t_api_nonce where czy_id=#{czyId} and ms=#{ms} and nonce=#{nonce}")
    int delNonce(@Param(value = "czyId") String czyId,@Param(value = "ms") String ms,@Param(value = "nonce") String nonce);

    //根据nonce取出日志里面使用过的，并且在有效期的记录
    @Select("select id from t_api_log where nonce=#{nonce} and end_date>=sysdate and czy_id=#{czyId} and ms=#{ms} and rownum=1")
    Long getNonceFromLog(@Param(value = "nonce") String nonce,@Param(value = "czyId") String czyId,@Param(value = "ms") String ms);

    @Select("select name from t_csb where code=#{code}")
    String getCsz(@Param(value = "code") String code);

    //获取 sysdate+#{expires_in}/24/60/60
    @Select("select to_char(sysdate+#{ms}/24/60/60,'yyyyMMddHH24mmss') from dual")
    String pickDateNowAddSecond(@Param(value = "ms") int ms);

    //获取当前时间
    @Select("select to_char(sysdate,'yyyyMMddHH24mmss') from dual")
    String pickDateNow();

    @Select("SELECT (SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24')) * 86400 FROM DUAL")
    String getTimestamp();

    @Select("select to_char(sysdate,'HH24:mi') from dual")
    String getTime();
}